<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Event;
use think\facade\View;

class Login extends Base
{
    protected $noNeedLogin = ['index','submit'];
    protected $noNeedRight = ['index','submit'];

    public function _initialize()
    {
        parent::_initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
    }


    public function index()
    {
        return View::fetch();
    }

    public function submit()
    {

        $url = $this->request->get('url', 'index/index');

        $username = $this->request->post('username','');
        $password = $this->request->post('password','');
        $captcha = $this->request->post('captcha','');

        $rule      = [
            'username|用户名' => 'require|length:3,30',
            'password|密码' => 'require|length:3,30',
            'captcha|验证码' => 'require|captcha',
        ];
        $data      = [
            'username'  => $username,
            'password'  => $password,
            'captcha'  => $captcha,
        ];



        $validate = validate($rule, [], false, false);
        $result   = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError(), $url);
        }
        $result = $this->auth->login($username, $password, 86400);
        if ($result === true) {
            $this->success('登陆成功', $url);
        } else {
            $msg = $this->auth->getError();
            $msg = $msg ?: "用户名或者密码错误";
            $this->error($msg, $url);
        }




    }

}